/*
 *  Note.cpp
 *  SoundController0002
 *
 *  Created by jiro on 11/10/02.
 *  Copyright 2011 jiro music. All rights reserved.
 *
 */

#include "AbstractNote.h"
//#include "../HarmonicSystem/HarmonicSystem.h"
#include "ConcreteNote.h"

namespace sc
{
namespace system0039
{

	AbstractNote::AbstractNote( Note::MidiNumber generateMidiNumber, Note::MidiNumber midiNumber, double dynamics )
		: generateMidiNumber( generateMidiNumber ),
		  midiNumber( midiNumber ),
		  dynamics( dynamics )
	{}

	sc::Note::MidiNumber AbstractNote::GetGenerateMidiNumber() const
	{
		return generateMidiNumber;
	}

	sc::Note::MidiNumber AbstractNote::GetMidiNumber() const
	{
		return midiNumber;
	}
	
	double AbstractNote::GetDynamics() const
	{
		return dynamics;
	}

	void AbstractNote::GetConcreteNotes( std::vector< ConcreteNote >& notes ) const
	{
		std::vector< ::sc::hs::HarmonicSound > harmonicSounds;
		System0039::GetConcreteNotesFrom( * this, harmonicSounds );

		notes.clear();
		for ( int i = 0, count = harmonicSounds.size(); i < count; i++ )
		{
			const ::sc::hs::HarmonicSound& sound = harmonicSounds[ i ];

			//	skip the same frequency note
			if ( WellTemperament::IsEqual( 
					WellTemperament::GetFrequency( GetMidiNumber() ), 
					sound.GetNthOfHarmonic() * WellTemperament::GetFrequency( GetGenerateMidiNumber() ) ) )
			{
				continue;
			}
			ConcreteNote note( 
				::sc::hs::Sound::SingleSineWave, GetMidiNumber(), 1.0, GetDynamics(),
				::sc::hs::Sound::SingleSineWave, GetGenerateMidiNumber(), sound.GetNthOfHarmonic(), GetDynamics() );
			notes.push_back( note );
		}
	}

	std::string AbstractNote::ToString( int tabCount ) const
	{
		std::stringstream ss;
		for ( int i = 0; i < tabCount; i++ )
		{
			ss << "\t";
		}
		ss << "AbstractNote: generator: "
		   << Note::ToString( generateMidiNumber )
		   << ", note: " << Note::ToString( midiNumber )
		   << ", dynamics: " << dynamics;
		return ss.str();
	}

}
}
